package com.wxw.BaseLibrary.drawable;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.Toast;
import com.wxw.BaseLibrary.tool.FileUtil;
import com.wxw.BaseLibrary.tool.L;
import com.wxw.BaseLibrary.tool.SystemTool;
import com.wxw.BaseLibrary.tool.ToolsDate;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Created by user on 2016/4/18.
 */
public class ImageTool {
    /**
     * 读取图片文件到Bitmap对象
     * @param path 图片路径
     * @param size 原图的几分之一
     * @return
     */
    public static Bitmap getBitmapFromPath(String path, int size) {
        Bitmap bm = null;
        try {
            FileInputStream f = new FileInputStream(path);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = size;//图片的长宽都是原来的1/8
            BufferedInputStream bis = new BufferedInputStream(f);
            bm = BitmapFactory.decodeStream(bis, null, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }

    public static Bitmap getBitmapFromPath(String path) {
        return getBitmapFromPath(path, 8);
    }

    public static Bitmap getBitmapFromFile(File file, int size) {
        return getBitmapFromPath(file.getPath(), size);
    }

    public static Bitmap getBitmapFromFile(File file) {
        return  getBitmapFromFile(file, 8);
    }

    public static Bitmap saveBitmap(Context context, Bitmap bitmap, String fileName, int quality) {
        return saveBitmap(context, bitmap, fileName, quality, "");
    }

    public static Bitmap saveBitmap(Context context, Bitmap bitmap, String fileName, int quality, String watermark) {
        return saveBitmap(context, bitmap, fileName, quality, watermark, 14);
    }

    public static Bitmap saveBitmap(Context context, Bitmap bitmap, String fileName, int quality,
                                    String watermark, int textSize) {
        return saveBitmap(context, bitmap, fileName, quality, watermark, textSize, Color.RED, 20);
    }

    public static Bitmap saveBitmap(Context context, Bitmap bitmap, String fileName, int quality,
                                    String watermark, int textSize, int padding) {
        return saveBitmap(context, bitmap, fileName, quality, watermark, textSize, Color.RED, padding);
    }

    public static Bitmap saveBitmap(Context context, Bitmap bitmap, String fileName, int quality,
                                    String watermark, int textSize, int textColor, int padding) {
        FileOutputStream outputStream = null;
        try {
            if (bitmap == null) {
                return null;
            }
            File file = new File(fileName);
            if (file.isDirectory()) {
                return bitmap;
            }
            FileUtil.createDirectory(file.getParent());
            outputStream = new FileOutputStream(fileName);
            if (watermark != null && !"".equals(watermark)) {
                bitmap = SystemTool.drawTextToBitmap(context, bitmap, watermark, textSize, textColor, padding);
            }
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        } catch (Exception e) {
            Toast.makeText(context, "保存照片错误:(" + e.getMessage() + ")", Toast.LENGTH_SHORT).show();
            L.WriteExceptionLog("ImageTool", "saveBitmap", e.getMessage());
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }
}
